package com.axios.controller.servlets;

import com.axios.dao.impl.JsonServerDAOImpl;
import com.axios.dao.interfaces.JsonServerDAO;
import com.axios.models.pojo.JsonServer;
import com.axios.service.JsonServerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateJsonServer")
public class JsonServerUpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应头允许跨域
        response.setHeader("Access-Control-Allow-Origin","*");

        // 响应头
        response.setHeader("Access-Control-Allow-Headers","*");

        // 接收参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");

        // 创建对象
        JsonServer jsonServer = new JsonServer(Integer.parseInt(id),name,sex,Integer.parseInt(age));

        // 执行修改操作
        int count = JsonServerService.update(Integer.parseInt(id),jsonServer);

        // 判断是否修改成功
        if(count == -1){
            response.getWriter().write("修改失败！");
            System.out.print(1);
        }else{
            response.getWriter().write("修改成功！");
            System.out.print(2);
        }
    }
}
